﻿#include <QMessageBox>
#include "CSensorItemConfig.h"
#include "CHeader.h"

#include "CLogOper.h"

CSensorItemConfig::CSensorItemConfig(QString sSceneGuid, CDevBase* pDev,QWidget *parent)
	: QWidget(parent)
{
	m_sGuid = sSceneGuid;
	m_pDev = pDev;
	ui.setupUi(this);
	initConnect();
	initData();
}

CSensorItemConfig::~CSensorItemConfig()
{}

void CSensorItemConfig::setData(QString sName, int nDevTypeCode, QString sItemGuid, int nAlarmType, double dDataThreadHoldHH, double dDataThreadHoldLL
	, double dDataThreadHoldH, double dDataThreadHoldL, int nBelong, int nCheckType,
	int nCollectCheckType, int nCollectRollerNum, int nCollectIndex, double dCollectInterval)
{
	ui.comboBoxDevTypeCode->setCurrentText(m_mapDevTypeCode.value(nDevTypeCode));
	ui.lineEditSensorItemName->setText(sName);
	ui.lineEditSensorGuid->setText(sItemGuid);
	ui.comboBoxAlarmLevel->setCurrentText(m_mapAlarmType.value(nAlarmType));
	ui.lineEditDataThreadHoldHH->setText(QString::number(dDataThreadHoldHH));
	ui.lineEditDataThreadHoldLL->setText(QString::number(dDataThreadHoldLL));
	ui.lineEditDataThreadHoldH->setText(QString::number(dDataThreadHoldH));
	ui.lineEditDataThreadHoldL->setText(QString::number(dDataThreadHoldL));
	ui.comboBoxBelong->setCurrentText(m_mapBelong.value(nBelong));
	ui.comboBoxCheckType->setCurrentText(m_mapCheckType.value(nCheckType));
	ui.comboBoxCollectIndex->setCurrentText(m_mapCollectIndex.value(nCollectIndex));
	if (nCollectCheckType == 0)
	{
		ui.comboBoxCollectCheckType->setCurrentText(A2T("根据米数判断"));
		ui.lineEditCollectInterval->setText(QString::number(dCollectInterval));
	}
	else if (nCollectCheckType == 1)
	{
		ui.comboBoxCollectCheckType->setCurrentText(A2T("根据托辊个数判断"));
		ui.lineEditCollectInterval->setText(QString::number(nCollectRollerNum));
	}
}

//设置标题
void CSensorItemConfig::setTitle()
{
	if (m_nType == 0)
	{
		ui.label_2->setText(A2T("增加传感器巡检项"));
	}
	else if (m_nType == 1)
	{
		ui.label_2->setText(A2T("修改传感器巡检项"));
	}
}

void CSensorItemConfig::initData()
{
	ui.labelItemGuid->hide();
	ui.lineEditSensorGuid->hide();
	m_mapDevTypeCode.insert(enCO, A2T("一氧化碳"));
	m_mapDevTypeCode.insert(enGas, A2T("可燃气体"));
	m_mapDevTypeCode.insert(enPM25, A2T("PM2.5"));
	m_mapDevTypeCode.insert(enPM10, A2T("PM10"));
	m_mapDevTypeCode.insert(enTemp, A2T("温度传感器"));
	m_mapDevTypeCode.insert(enHum, A2T("湿度传感器"));
	m_mapDevTypeCode.insert(enUltrasonic, A2T("超声波传感器"));
	ui.comboBoxDevTypeCode->addItem(A2T("一氧化碳"));
	ui.comboBoxDevTypeCode->addItem(A2T("可燃气体"));
	ui.comboBoxDevTypeCode->addItem(A2T("PM2.5"));
	ui.comboBoxDevTypeCode->addItem(A2T("PM10"));
	ui.comboBoxDevTypeCode->addItem(A2T("温度传感器"));
	ui.comboBoxDevTypeCode->addItem(A2T("湿度传感器"));
	ui.comboBoxDevTypeCode->addItem(A2T("超声波传感器"));

	ui.comboBoxAlarmLevel->addItem(A2T("一级"));
	ui.comboBoxAlarmLevel->addItem(A2T("二级"));
	ui.comboBoxAlarmLevel->addItem(A2T("三级"));
	m_mapAlarmType.insert(1, A2T("一级"));
	m_mapAlarmType.insert(2, A2T("二级"));
	m_mapAlarmType.insert(3, A2T("三级"));

	m_mapBelong.insert(0, A2T("A侧"));
	m_mapBelong.insert(1, A2T("B侧"));
	ui.comboBoxBelong->addItem(A2T("A侧"));
	ui.comboBoxBelong->addItem(A2T("B侧"));

	ui.comboBoxCheckType->addItem(A2T("调用返回"));
	ui.comboBoxCheckType->addItem(A2T("持续任务主动上报"));
	m_mapCheckType.insert(0, A2T("调用返回"));
	m_mapCheckType.insert(1, A2T("持续任务主动上报"));

	ui.comboBoxCollectIndex->addItem(A2T("起点"));
	ui.comboBoxCollectIndex->addItem(A2T("终点"));
	ui.comboBoxCollectIndex->addItem(A2T("中间点"));
	m_mapCollectIndex.insert(0, A2T("起点"));
	m_mapCollectIndex.insert(1, A2T("终点"));
	m_mapCollectIndex.insert(2, A2T("中间点"));

	m_mapCollectCheckType.insert(0, A2T("根据米数判断"));
	m_mapCollectCheckType.insert(1, A2T("根据托辊个数判断"));
	ui.comboBoxCollectCheckType->addItem(A2T("根据米数判断"));
	ui.comboBoxCollectCheckType->addItem(A2T("根据托辊个数判断"));
}

void CSensorItemConfig::initConnect()
{
	connect(ui.pushButtonSave, &QPushButton::clicked, this, &CSensorItemConfig::slotPushButtonSaveClicked);
	connect(ui.comboBoxDevTypeCode, SIGNAL(currentIndexChanged(int)), this, SLOT(slotDevTypeCodeChanged(int)));
	connect(ui.comboBoxCheckType, SIGNAL(currentIndexChanged(int)), this, SLOT(slotCheckTypeChanged(int)));
	connect(ui.comboBoxCollectCheckType, SIGNAL(currentIndexChanged(int)), this, SLOT(slotCollectCheckTypeChanged(int)));
	connect(ui.pushButton_Alarm, &QPushButton::clicked, this, &CSensorItemConfig::slotAlarmSet);
}

void CSensorItemConfig::slotPushButtonSaveClicked()
{
	int nResult = messageBox(3, A2T("是否确认此操作?"));
	if (nResult == QMessageBox::No || QMessageBox::Close == nResult)
	{
		return;
	}
	if (m_nType == 0)//增加
	{
		QString sGuid = createGuid();
		//生效了就刷新全局的数据
		CPolItemSensor* pItem = new CPolItemSensor();
		pItem->m_sItemGuid = sGuid;
		pItem->m_sPointGuid = m_sPointGuid;
		pItem->m_sGuid = m_sGuid;
		pItem->m_nDevType = 0;//本体(传感器)
		pItem->m_nItemStatus = 1;
		pItem->m_sItemName = ui.lineEditSensorItemName->text();
		pItem->m_nDevTypeCode = m_mapDevTypeCode.key(ui.comboBoxDevTypeCode->currentText());
		pItem->m_nAlarmLevel = m_mapAlarmType.key(ui.comboBoxAlarmLevel->currentText());
		pItem->m_sDataThreadHoldH = ui.lineEditDataThreadHoldH->text();
		pItem->m_sDataThreadHoldHH = ui.lineEditDataThreadHoldHH->text();
		pItem->m_sDataThreadHoldL = ui.lineEditDataThreadHoldL->text();
		pItem->m_sDataThreadHoldLL = ui.lineEditDataThreadHoldLL->text();
		pItem->m_nDevType = 0;//本体
		pItem->m_nItemStatus = 1;
		if (m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_nPointStatus == 0)
		{
			m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_nPointStatus = 2;
		}
		else if (m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_nPointStatus == 1)
		{
			m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_nPointStatus = 1;
		}
		else if (m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_nPointStatus == 2)
		{
			m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_nPointStatus = 2;
		}
		pItem->m_nIndex= m_mapDevTypeCode.key(ui.comboBoxDevTypeCode->currentText());
		pItem->m_nBelong = m_mapBelong.key(ui.comboBoxBelong->currentText());
		pItem->m_nCheckType = m_mapCheckType.key(ui.comboBoxCheckType->currentText());
		pItem->m_nCollectIndex = m_mapCollectIndex.key(ui.comboBoxCollectIndex->currentText());
		pItem->m_nCollectCheckType = m_mapCollectCheckType.key(ui.comboBoxCollectCheckType->currentText());
		if (pItem->m_nCollectCheckType == 0)
		{
			pItem->m_dCollectInterval = ui.lineEditCollectInterval->text().toDouble();
			pItem->m_nCollectRollerNum = 0;
		}
		else
		{
			pItem->m_nCollectRollerNum = ui.lineEditCollectInterval->text().toInt();
			pItem->m_dCollectInterval = 0;
		}
		m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_mapPolItem.insert(sGuid, pItem);
		//通知添加成功
		messageBox(0, A2T("添加成功"));
		//SExecLog sLog;
		//sLog.m_dLogTime = QDateTime::currentDateTime();
		//sLog.m_nAlarmLevel = enWorkInfo;
		//sLog.m_sDescriptionl = A2T("%1添加了%2名为%3的传感器巡检项")
		//	.arg(CPublicData::instance().m_userInfo.sUserName)
		//	.arg(CPublicData::instance().m_mapIFScene[m_sGuid]->m_sSceneName.get())
		//	.arg(ui.lineEditSensorItemName->text());
		//sLog.m_sDevName = A2T("巡检项");
		//CLogOper::instance().addMsg(m_sGuid, sLog);
		//通知上层刷新界面
		emit(sigRefushUI());
		this->close();
	}
	else if (m_nType == 1)//修改
	{
		CPolItemSensor* pItem = (CPolItemSensor*)m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_mapPolItem[ui.lineEditSensorGuid->text()];
		pItem->m_sItemName = ui.lineEditSensorItemName->text();
		pItem->m_nAlarmLevel = m_mapAlarmType.key(ui.comboBoxAlarmLevel->currentText());
		pItem->m_nDevTypeCode = m_mapDevTypeCode.key(ui.comboBoxDevTypeCode->currentText());
		pItem->m_sDataThreadHoldH = ui.lineEditDataThreadHoldH->text();
		pItem->m_sDataThreadHoldHH = ui.lineEditDataThreadHoldHH->text();
		pItem->m_sDataThreadHoldL = ui.lineEditDataThreadHoldL->text();
		pItem->m_sDataThreadHoldLL = ui.lineEditDataThreadHoldLL->text();
		pItem->m_nDevType = 0;//本体
		pItem->m_nBelong = m_mapBelong.key(ui.comboBoxBelong->currentText());
		pItem->m_nCheckType = m_mapCheckType.key(ui.comboBoxCheckType->currentText());
		pItem->m_nCollectIndex = m_mapCollectIndex.key(ui.comboBoxCollectIndex->currentText());
		pItem->m_nCollectCheckType = m_mapCollectCheckType.key(ui.comboBoxCollectCheckType->currentText());
		if (pItem->m_nCollectCheckType == 0)
		{
			pItem->m_dCollectInterval = ui.lineEditCollectInterval->text().toDouble();
			pItem->m_nCollectRollerNum = 0;
		}
		else
		{
			pItem->m_nCollectRollerNum = ui.lineEditCollectInterval->text().toInt();
			pItem->m_dCollectInterval = 0;
		}
		if (pItem->m_nItemStatus != 1)
		{
			pItem->m_nItemStatus = 2;
		}
		if (m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_nPointStatus == 0)
		{
			m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_nPointStatus = 2;
		}
		else if (m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_nPointStatus == 1)
		{
			m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_nPointStatus = 1;
		}
		else if (m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_nPointStatus == 2)
		{
			m_pPolAreaOld->m_mapPolPoint[m_sPointGuid]->m_nPointStatus = 2;
		}
		//通知添加成功
		messageBox(0, A2T("修改成功"));
		//SExecLog sLog;
		//sLog.m_dLogTime = QDateTime::currentDateTime();
		//sLog.m_nAlarmLevel = enWorkInfo;
		//sLog.m_sDescriptionl = A2T("%1添加了%2名为%3的传感器巡检项")
		//	.arg(CPublicData::instance().m_userInfo.sUserName)
		//	.arg(CPublicData::instance().m_mapIFScene[m_sGuid]->m_sSceneName.get())
		//	.arg(ui.lineEditSensorItemName->text());
		//sLog.m_sDevName = A2T("巡检项");
		//CLogOper::instance().addMsg(m_sGuid, sLog);
		//通知上层刷新界面
		emit(sigRefushUI());
		this->close();
	}
	return;
}

void CSensorItemConfig::slotDevTypeCodeChanged(int nIndex)
{
	if (ui.comboBoxDevTypeCode->currentText() == A2T("一氧化碳"))
	{
		ui.labelDataThreadHoldH->show();
		ui.labelDataThreadHoldH->setText(A2T("报警上限(ppm):"));
		ui.labelDataThreadHoldHH->show();
		ui.labelDataThreadHoldHH->setText(A2T("报警上上限(ppm):"));
		ui.labelDataThreadHoldL->hide();
		ui.labelDataThreadHoldL->setText(A2T("报警下限(ppm):"));
		ui.labelDataThreadHoldLL->hide();
		ui.labelDataThreadHoldLL->setText(A2T("报警下下限(ppm):"));
		ui.lineEditDataThreadHoldHH->show();
		ui.lineEditDataThreadHoldHH->setText(A2T("42"));
		ui.lineEditDataThreadHoldH->show();
		ui.lineEditDataThreadHoldH->setText(A2T("32"));
		ui.lineEditDataThreadHoldLL->hide();
		ui.lineEditDataThreadHoldLL->setText(A2T("-9999"));
		ui.lineEditDataThreadHoldL->hide();
		ui.lineEditDataThreadHoldL->setText(A2T("-9999"));
		ui.lineEditSensorItemName->setText(A2T("一氧化碳巡检项"));
	}
	else if (ui.comboBoxDevTypeCode->currentText() == A2T("可燃气体"))
	{
		ui.labelDataThreadHoldH->show();
		ui.labelDataThreadHoldH->setText(A2T("报警上限(ppm):"));
		ui.labelDataThreadHoldHH->show();
		ui.labelDataThreadHoldHH->setText(A2T("报警上上限(ppm):"));
		ui.labelDataThreadHoldL->hide();
		ui.labelDataThreadHoldL->setText(A2T("报警下限(ppm):"));
		ui.labelDataThreadHoldLL->hide();
		ui.labelDataThreadHoldLL->setText(A2T("报警下下限(ppm):"));
		ui.lineEditDataThreadHoldHH->show();
		ui.lineEditDataThreadHoldHH->setText(A2T("35"));
		ui.lineEditDataThreadHoldH->show();
		ui.lineEditDataThreadHoldH->setText(A2T("25"));
		ui.lineEditDataThreadHoldLL->hide();
		ui.lineEditDataThreadHoldLL->setText(A2T("-9999"));
		ui.lineEditDataThreadHoldL->hide();
		ui.lineEditDataThreadHoldL->setText(A2T("-9999"));
		ui.lineEditSensorItemName->setText(A2T("可燃气体巡检项"));
	}
	else if (ui.comboBoxDevTypeCode->currentText() == A2T("PM2.5"))
	{
		ui.labelDataThreadHoldH->show();
		ui.labelDataThreadHoldH->setText(A2T("报警上限(ug/m3):"));
		ui.labelDataThreadHoldHH->show();
		ui.labelDataThreadHoldHH->setText(A2T("报警上上限(ug/m3):"));
		ui.labelDataThreadHoldL->hide();
		ui.labelDataThreadHoldL->setText(A2T("报警下限(ug/m3):"));
		ui.labelDataThreadHoldLL->hide();
		ui.labelDataThreadHoldLL->setText(A2T("报警下下限(ug/m3):"));
		ui.lineEditDataThreadHoldHH->show();
		ui.lineEditDataThreadHoldHH->setText(A2T("125"));
		ui.lineEditDataThreadHoldH->show();
		ui.lineEditDataThreadHoldH->setText(A2T("115"));
		ui.lineEditDataThreadHoldLL->hide();
		ui.lineEditDataThreadHoldLL->setText(A2T("-9999"));
		ui.lineEditDataThreadHoldL->hide();
		ui.lineEditDataThreadHoldL->setText(A2T("-9999"));
		ui.lineEditSensorItemName->setText(A2T("PM2.5巡检项"));
	}
	else if (ui.comboBoxDevTypeCode->currentText() == A2T("PM10"))
	{
		ui.labelDataThreadHoldH->show();
		ui.labelDataThreadHoldH->setText(A2T("报警上限(ug/m3):"));
		ui.labelDataThreadHoldHH->show();
		ui.labelDataThreadHoldHH->setText(A2T("报警上上限(ug/m3):"));
		ui.labelDataThreadHoldL->hide();
		ui.labelDataThreadHoldL->setText(A2T("报警下限(ug/m3):"));
		ui.labelDataThreadHoldLL->hide();
		ui.labelDataThreadHoldLL->setText(A2T("报警下下限(ug/m3):"));
		ui.lineEditDataThreadHoldHH->show();
		ui.lineEditDataThreadHoldHH->setText(A2T("260"));
		ui.lineEditDataThreadHoldH->show();
		ui.lineEditDataThreadHoldH->setText(A2T("250"));
		ui.lineEditDataThreadHoldLL->hide();
		ui.lineEditDataThreadHoldLL->setText(A2T("-9999"));
		ui.lineEditDataThreadHoldL->hide();
		ui.lineEditDataThreadHoldL->setText(A2T("-9999"));
		ui.lineEditSensorItemName->setText(A2T("PM10巡检项"));
	}
	else if (ui.comboBoxDevTypeCode->currentText() == A2T("温度传感器"))
	{
		ui.labelDataThreadHoldH->show();
		ui.labelDataThreadHoldH->setText(A2T("报警上限(℃):"));
		ui.labelDataThreadHoldHH->show();
		ui.labelDataThreadHoldHH->setText(A2T("报警上上限(℃):"));
		ui.labelDataThreadHoldL->hide();
		ui.labelDataThreadHoldL->setText(A2T("报警下限(℃):"));
		ui.labelDataThreadHoldLL->hide();
		ui.labelDataThreadHoldLL->setText(A2T("报警下下限(℃):"));
		ui.lineEditDataThreadHoldHH->show();
		ui.lineEditDataThreadHoldHH->setText(A2T("60"));
		ui.lineEditDataThreadHoldH->show();
		ui.lineEditDataThreadHoldH->setText(A2T("50"));
		ui.lineEditDataThreadHoldLL->hide();
		ui.lineEditDataThreadHoldLL->setText(A2T("-10"));
		ui.lineEditDataThreadHoldL->hide();
		ui.lineEditDataThreadHoldL->setText(A2T("-5"));
		ui.lineEditSensorItemName->setText(A2T("温度巡检项"));
	}
	else if (ui.comboBoxDevTypeCode->currentText() == A2T("湿度传感器"))
	{
		ui.labelDataThreadHoldH->show();
		ui.labelDataThreadHoldH->setText(A2T("报警上限(%RH):"));
		ui.labelDataThreadHoldHH->show();
		ui.labelDataThreadHoldHH->setText(A2T("报警上上限(%RH):"));
		ui.labelDataThreadHoldL->hide();
		ui.labelDataThreadHoldL->setText(A2T("报警下限(%RH):"));
		ui.labelDataThreadHoldLL->hide();
		ui.labelDataThreadHoldLL->setText(A2T("报警下下限(%RH):"));
		ui.lineEditDataThreadHoldHH->show();
		ui.lineEditDataThreadHoldHH->setText(A2T("80"));
		ui.lineEditDataThreadHoldH->show();
		ui.lineEditDataThreadHoldH->setText(A2T("70"));
		ui.lineEditDataThreadHoldLL->hide();
		ui.lineEditDataThreadHoldLL->setText(A2T("-9999"));
		ui.lineEditDataThreadHoldL->hide();
		ui.lineEditDataThreadHoldL->setText(A2T("-9999"));
		ui.lineEditSensorItemName->setText(A2T("湿度巡检项"));
	}
	else if (ui.comboBoxDevTypeCode->currentText() == A2T("超声波传感器"))
	{
		ui.labelDataThreadHoldH->show();
		ui.labelDataThreadHoldH->setText(A2T("报警上限:"));
		ui.labelDataThreadHoldHH->show();
		ui.labelDataThreadHoldHH->setText(A2T("报警上上限:"));
		ui.labelDataThreadHoldL->hide();
		ui.labelDataThreadHoldL->setText(A2T("报警下限:"));
		ui.labelDataThreadHoldLL->hide();
		ui.labelDataThreadHoldLL->setText(A2T("报警下下限:"));
		ui.lineEditDataThreadHoldHH->show();
		ui.lineEditDataThreadHoldHH->setText(A2T("120"));
		ui.lineEditDataThreadHoldH->show();
		ui.lineEditDataThreadHoldH->setText(A2T("100"));
		ui.lineEditDataThreadHoldLL->hide();
		ui.lineEditDataThreadHoldLL->setText(A2T("-9999"));
		ui.lineEditDataThreadHoldL->hide();
		ui.lineEditDataThreadHoldL->setText(A2T("-9999"));
		ui.lineEditSensorItemName->setText(A2T("超声波巡检项"));
	}
}

void CSensorItemConfig::slotCheckTypeChanged(int nIndex)
{
	if (ui.comboBoxCheckType->currentText() == A2T("调用返回"))
	{
		ui.labelCollectIndex->hide();
		ui.comboBoxCollectIndex->hide();
		ui.labelCollectInterval->hide();
		ui.lineEditCollectInterval->hide();
		ui.labelCollectCheckType->hide();
		ui.comboBoxCollectCheckType->hide();
	}
	else if (ui.comboBoxCheckType->currentText() == A2T("持续任务主动上报"))
	{
		ui.labelCollectIndex->show();
		ui.comboBoxCollectIndex->show();
		ui.labelCollectInterval->show();
		ui.lineEditCollectInterval->show();
		ui.labelCollectCheckType->show();
		ui.comboBoxCollectCheckType->show();
	}
}

void CSensorItemConfig::slotCollectCheckTypeChanged(int nIndex)
{
	if (ui.comboBoxCollectCheckType->currentText() == A2T("根据米数判断"))
	{
		ui.labelCollectInterval->setText(A2T("持续监测报警间隔距离(m):"));
		ui.lineEditCollectInterval->setText(QString::number(2));
	}
	else if (ui.comboBoxCollectCheckType->currentText() == A2T("根据托辊个数判断"))
	{
		ui.labelCollectInterval->setText(A2T("持续监测报警间隔托辊个数:"));
		ui.lineEditCollectInterval->setText(QString::number(1));
	}
}

void CSensorItemConfig::slotAlarmSet()
{
	//ShowDetecStateBind(m_pDev->m_sDevGuid, ui.lineEditSensorGuid->text(), 0);
}
